package org.fujay.langchain4jserver.infrastructure.persistence.assembler;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.fujay.langchain4jserver.domain.knowledge.entity.File;
import org.fujay.langchain4jserver.domain.knowledge.entity.KnowledgeBase;
import org.fujay.langchain4jserver.domain.knowledge.vo.*;
import org.fujay.langchain4jserver.domain.model.vo.ModelId;
import org.fujay.langchain4jserver.infrastructure.persistence.mybatis.entity.KnowledgeBasePo;
import org.fujay.langchain4jserver.infrastructure.persistence.mybatis.entity.KnowledgeFilePo;

/**
 * @author slm
 * @description
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class KnowledgeAssembler {

    public static KnowledgeBasePo convertKbToPO(final KnowledgeBase kb) {
        if (kb == null) return null;

        KnowledgeBasePo po = new KnowledgeBasePo();
        po.setId(kb.getId().value());
        po.setName(kb.getName());
        po.setDescription(kb.getDescription());
        po.setIndexName(kb.getIndexName());
        po.setEmbeddingModelId(kb.getEmbeddingModelId().value());
        po.setNamespace(kb.getNamespace());
        po.setStatus(kb.getStatus().getCode());
        po.setDocumentCount(kb.getDocumentCount().value());

        return po;
    }

    public static KnowledgeFilePo convertToPO(
            final File file, final KnowledgeBaseId knowledgeBaseId) {
        if (file == null) return null;

        KnowledgeFilePo po = new KnowledgeFilePo();
        po.setId(file.getId().value());
        po.setKnowledgeBaseId(knowledgeBaseId.value()); // 需要传入关联的 KB ID
        po.setFileName(file.getFileName());
        po.setFilePath(file.getFilePath());
        po.setFileSize(file.getFileSize());
        po.setFileType(file.getFileType());
        po.setStorageStrategy(file.getStorageStrategy().getCode());
        po.setUploadTime(file.getUploadTime());
        po.setIndexStatus(file.getIndexStatus().getCode());
        po.setErrorMessage(file.getErrorMessage());

        return po;
    }

    public static KnowledgeBase convertKbToDomain(final KnowledgeBasePo po) {

        KnowledgeBaseId kbId = new KnowledgeBaseId(po.getId());
        ModelId modelId = new ModelId(po.getEmbeddingModelId());
        KnowledgeBaseStatus status = KnowledgeBaseStatus.fromCode(po.getStatus());
        DocumentCount count = new DocumentCount(po.getDocumentCount());

        return KnowledgeBase.load(
                kbId,
                po.getName(),
                po.getDescription(),
                po.getIndexName(),
                modelId,
                po.getNamespace(),
                status,
                count,
                po.getCreateTime(),
                po.getUpdateTime());
    }

    public static File convertToDomain(KnowledgeFilePo po) {

        FileId fileId = new FileId(po.getId());
        StorageStrategy strategy = StorageStrategy.fromCode(po.getStorageStrategy());
        IndexStatus status = IndexStatus.fromCode(po.getIndexStatus());

        return File.load(
                fileId,
                new KnowledgeBaseId(po.getKnowledgeBaseId()),
                po.getFileName(),
                po.getFilePath(),
                po.getFileSize(),
                po.getFileType(),
                strategy,
                po.getUploadTime(),
                status,
                po.getErrorMessage(),
                po.getCreateTime(),
                po.getUpdateTime());
    }
}
